package com.zoe.onelink.auth.token.processor.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.zoe.onelink.auth.token.processor.OnelinkTokenInfoProcessor;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-05-12
 */
public class OnelinkJsonTokenInfoProcessor implements OnelinkTokenInfoProcessor {

    private final AES aes;

    public OnelinkJsonTokenInfoProcessor(String aesKey) {
        Assert.notNull(aesKey, "[ONELINK-TOKEN] DES 密钥不能为空");
        aes = SecureUtil.aes(aesKey.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public Map<String, Object> process(String onelinkToken) {
        String authStr = this.aes.decryptStr(onelinkToken);
        // 由于旧版本兼容原因,该实现类默认只支持Map<String,String>类型,需要其他类型请重新实现 OnelinkTokenInfoProcessor
        return JSON.parseObject(authStr, HashMap.class);
    }

}
